package cc.vicp.djx314.starfood.util.impls;

import java.util.Map;

import org.springframework.util.Assert;

import cc.vicp.djx314.starfood.util.DaysOfWeekTran;

/**
 * 星期数和数字的转换类
 * @author 水山清风
 *
 */
public class DaysOfWeekTranImpl implements DaysOfWeekTran {
	private Map<Integer, String> weekIndexMap;
	public void setWeekIndexMap(Map<Integer, String> weekIndexMap) {
		this.weekIndexMap = weekIndexMap;
	}

	public String getWeekDay(final Integer dayIndex) {
		String weekDay = weekIndexMap.get(dayIndex);
		Assert.notNull(weekDay, "找不到索引" + dayIndex + "对应的星期数");
		return weekDay;
	}

	public Integer getWeekDayIndex(final String weekDay) {
		String resultDay = null;
		Integer myDayIndex = null;
		for (Integer dayIndex : weekIndexMap.keySet()) {
			resultDay = weekIndexMap.get(dayIndex);
			if (weekDay.equals(resultDay)) {
				myDayIndex = dayIndex;
				break;
			}
		}
		Assert.notNull(myDayIndex, "找不到星期 " + weekDay + " 对应的索引");
		return myDayIndex;
	}

	public Integer weekDayToCalendar(Integer weekDay) {
		int offset = 1;
		Integer result = null;
		int plusWeekDay = weekDay + 7 + offset;
		if (weekDay != null) {
			result = (plusWeekDay % 7 == 0) ? 7 : (Integer) (plusWeekDay % 7);
		}
		return result;
	};
}